package com.miracle9.game.dao;

import com.miracle9.game.entity.Promoter;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class PromoterServiceDao1 extends SqlOperating<Promoter, Integer> {

    public boolean getQuantityByUserName(String username) {
        return q_CreateSqlInt("select count(*) from Promoter where username=?", new Object[]{username}) > 0;
    }

    public void updateState_1ById(int id) {
        q_CreateSql("update Promoter set state = 1 where id = ?", new Object[]{Integer.valueOf(id)}).executeUpdate();
    }

    public void updateState_0ById(int id) {
        q_CreateSql("update Promoter set state = 0 where id = ?", new Object[]{Integer.valueOf(id)}).executeUpdate();
    }

    public void addPromoter(Promoter promoter) {
        saveReturn(promoter);
    }

    public Integer updatePromoterInfo(Promoter deletePromoter) {
        q_CreateSql("update Promoter set parentId=?,parentName=?,level=? where parentId=?", new Object[]{Integer.valueOf(deletePromoter.getParentId()), deletePromoter.getParentName(), Integer.valueOf(deletePromoter.getLevel()), Integer.valueOf(deletePromoter.getId())}).executeUpdate();
        List<Promoter> subPromoters = q_CreateSqlList("from Promoter where parentId=?", new Object[]{Integer.valueOf(deletePromoter.getParentId())});
        if (!subPromoters.isEmpty()) {
            for (Promoter p : subPromoters) {
                updateLevelAndGetPromoters(p);
            }
        }
        return null;
    }

    public void updatePromoterInfoAndGetPromoters(int parentId, Promoter targetPromoter) {
        int promoterCount = q_CreateSql("update Promoter set parentId=?,parentName=?,level=? where parentId=?", new Object[]{
                Integer.valueOf(targetPromoter.getId()), targetPromoter.getUsername(), Integer.valueOf(targetPromoter.getLevel() + 1), Integer.valueOf(parentId)})
                .executeUpdate();
        targetPromoter.setChildrenPromoterNum(targetPromoter.getChildrenPromoterNum() + promoterCount);
        List<Promoter> subPromoters = q_CreateSqlList("from Promoter where parentId=?", new Object[]{Integer.valueOf(targetPromoter.getId())});
        if (!subPromoters.isEmpty()) {
            for (Promoter p : subPromoters) {
                updateLevelAndGetPromoters(p);
            }
        }
    }

    public void updateLevelAndGetPromoters(Promoter parent) {
        q_CreateSql("update Promoter set level=? where parentId=?", new Object[]{Integer.valueOf(parent.getLevel() + 1), Integer.valueOf(parent.getId())}).executeUpdate();
        List<Promoter> subPromoters = q_CreateSqlList("from Promoter where parentId=?", new Object[]{Integer.valueOf(parent.getId())});
        if (!subPromoters.isEmpty()) {
            for (Promoter p : subPromoters) {
                updateLevelAndGetPromoters(p);
            }
        }
    }

    public void updatePasswordById(int id, String strUpdatePassword) {
        q_CreateSql("update Promoter set password=? where id=?", new Object[]{strUpdatePassword, Integer.valueOf(id)}).executeUpdate();
    }

    public void updateRemarkById(int id, String remark) {
        q_CreateSql("update Promoter set remark=? where id=?", new Object[]{remark, Integer.valueOf(id)}).executeUpdate();
    }

    public void updateKefuById(int id, String kefu) {
        q_CreateSql("update Promoter set kefu=? where id=?", new Object[]{kefu, Integer.valueOf(id)}).executeUpdate();
    }

    public void updatePromoterAuth(int id, int goldAuth, int fishAuth) {
        q_CreateSql("update Promoter set goldAuth=?,fishAuth=? where id=?", new Object[]{goldAuth, fishAuth, Integer.valueOf(id)}).executeUpdate();
    }

    public void updatePayMoney(int id, int payMoney) {
        q_CreateSql("update Promoter set payMoney=? where id=?", new Object[]{Integer.valueOf(payMoney), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateExpiryNum(int id, int expiryNum) {
        q_CreateSql("update Promoter set expiryNum=? where id=?", new Object[]{Integer.valueOf(expiryNum), Integer.valueOf(id)}).executeUpdate();
    }

    public boolean addGold(int id, int gold) {
        getSession().flush();
        getSession().clear();
        List<Object> params = new ArrayList();
        String hql = "update Promoter set gold = gold+? where id = ?";
        params.add(Integer.valueOf(gold));
        params.add(Integer.valueOf(id));
        return q_CreateSql(hql, params.toArray()).executeUpdate() > 0;
    }

    public long getSumGoldByparentId_0() {
        Object sum = q_CreateSql("select sum(gold) from Promoter where parentId=0", new Object[0]).uniqueResult();
        if (sum == null) {
            return 0L;
        }
        return ((Long) sum).longValue();
    }

    public long getSumGoldByparentId() {
        Object sum = q_CreateSql("select sum(gold) from Promoter where parentId!=0", new Object[0]).uniqueResult();
        if (sum == null) {
            return 0L;
        }
        return ((Long) sum).longValue();
    }

    public int getQuantity() {
        return q_CreateSqlInt("select count(*) from Promoter", new Object[0]);
    }

    public void getPromotersByParentId(List<Integer> list, int parentId) {
        List<Promoter> promoters = q_CreateSqlList("from Promoter where parentId=?", new Object[]{Integer.valueOf(parentId)});
        for (Promoter p : promoters) {
            list.add(Integer.valueOf(p.getId()));
            getPromotersByParentId(list, p.getId());
        }
    }

    public List<Object[]> getListByInterval(String hql, String startDate, String endDate) {
        SQLQuery query = null;
        query = s_CreateSql(hql, new Object[]{startDate, endDate});
        return query.list();
    }

    public long getSumGold() {
        Object sum = q_CreateSql("select sum(gold) from Promoter", new Object[0]).uniqueResult();
        if (sum == null) {
            return 0L;
        }
        return ((Long) sum).longValue();
    }

    public int getQuantityByParentId_0() {
        return q_CreateSqlInt("select count(*) from Promoter where parentId = 0", new Object[0]);
    }

    public int getQuantityByParentId() {
        return q_CreateSqlInt("select count(*) from Promoter where parentId != 0", new Object[0]);
    }

}
